package fr.norsys.mapper.console.service;

import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import fr.norsys.mapper.console.exception.PropertyException;

public class PropertyServiceImpl implements PropertyService {
	private final static Log log = LogFactory.getLog(PropertyServiceImpl.class);
	public void clean(String pathFile) throws PropertyException {
		PrintWriter writer;
		try {
			writer = new PrintWriter(new FileWriter(pathFile));
		} catch (IOException e) {
			throw new PropertyException(e.toString());
		}
		writer.print("");
		writer.close();
	}

	/**
	 * Get the content of a property file as a Map
	 * @param pathFile name of the property file
	 * @return Map
	 * @throws PropertyException 
	 */
	public Map getProperty(String pathFile) throws PropertyException {
		Map properties = null;
		BufferedReader reader=null; 
		try {
			reader=  new BufferedReader(new FileReader(pathFile));
			String key;
			String value;
			int equalIdx;
			properties = new HashMap();
			
			String line = reader.readLine();
			while (line!=null){
				equalIdx = line.indexOf("=");
				if(equalIdx>0){
					key = line.substring(0,equalIdx).trim();
					value = line.substring(equalIdx+1);
					properties.put(key,value);
				}else{
					if(log.isDebugEnabled()) {
						log.debug("the line :\n"+line+"\nis not a property");
					}
				}
				line=reader.readLine();
			}
			
			if(reader!=null){
				reader.close();
			}
		} catch (FileNotFoundException e) {
			log.error("file " + pathFile+ " could not be found:" +e);
			try {
				if(reader!=null){
					reader.close();
				}
			} catch (IOException e1) {
				throw new PropertyException(e1.toString());
			}
			throw new PropertyException(e.toString());
		} catch (IOException e) {
			log.error(e);
			try {
				if(reader!=null){
					reader.close();
				}
			} catch (IOException e1) {
				throw new PropertyException(e1.toString());
			}
			throw new PropertyException(e.toString());
		}
		return properties;
	}

	/**
	 * Add property in a property file
	 * @param pathFile name of the property file
	 * @param map values to define
	 * @return Map
	 */
	public void setProperty(String pathFile, Map map) throws PropertyException {
		Map properties = getProperty(pathFile);
		String key;
		if (properties==null){
			if(log.isDebugEnabled()) {
				log.debug("a new file is created: " + pathFile);
			}
			properties = new HashMap();
		} else {
			if(properties.size()>0){
				for (Iterator i=properties.keySet().iterator();i.hasNext();){
					key=(String)i.next();
					if (map.containsKey(key) && log.isWarnEnabled()) {
						{log.warn("key " + key + " already exists with value " + properties.get(key));}
					}
				}
			}else{
				if(log.isDebugEnabled()) {
					log.debug("file is empty");
				}
			}
		}
		
		properties.putAll(map);
		Set keys = properties.keySet();
		
		try {
			PrintWriter writer = new PrintWriter(new FileWriter(pathFile));
			
			for(Iterator i=keys.iterator();i.hasNext();){
				key = (String)i.next();
				if(log.isDebugEnabled()) {
					log.debug(key+"="+properties.get(key));
				}
				writer.println(key+"="+properties.get(key));
			}
			writer.close();

		} catch (IOException e) {
			log.error(e);
			throw new PropertyException(e.toString());
		}
	}
}
